#include "Kernel_PCH.h"
#include "Log.h"

/*--------------------------------------------------------------------------*/

ILog* g_Log = NULL;

/*--------------------------------------------------------------------------*/

CLog::CLog()
{
// 	system("dir");
// 
// 	if (m_log == NULL)
// 	{
// 		errno_t err = 0;  
// 		err = fopen_s(&m_log, "./log.txt", "wb");
// 
// // 		if (m_log)
// // 		{
// // 			fprintf(m_log, LOG_HTML_BEGIN);
// // 		}
// 	}
}

/*--------------------------------------------------------------------------*/

CLog::~CLog()
{

}

/*--------------------------------------------------------------------------*/

static char messageboxBuffer[4096];
static char logfileBuffer[4096];
static char vsdebugBuffer[4096];

void CLog::Log(const char* file, int line, const char* func, const char* code, int priority, const char* format, ...)
{
	va_list args;
	va_start(args, format);
	vsprintf(logfileBuffer, format, args);
	perror(logfileBuffer);
	va_end(args);

	vsdebugBuffer[0] = 0;
	sprintf(vsdebugBuffer, "[ViaMain] >>> %s <<<\n", logfileBuffer);

	OutputDebugString(vsdebugBuffer);

// 	if (m_log)
// 	{
		if (priority == 0) //info
		{
			//std::cout << "#c" << logfileBuffer << "\n";
			//WriteLog(file, line, func, priority, logfileBuffer);
		}
		else if (priority == 1) //warning
		{
			//std::cout << "#y" << logfileBuffer << "\n";
			//WriteLog(file, line, func, priority, logfileBuffer);
		}
		else if (priority == 2) //error
		{
			//std::cout << "#r" << logfileBuffer << "\n";
			//WriteLog(file, line, func, priority, logfileBuffer);
			//DebugBreak();
		}
		else //critical
		{
			//std::cout << "#r" << logfileBuffer << "\n";
			//WriteLog(file, line, func, priority, logfileBuffer);

			sprintf(messageboxBuffer, "In file:\n%s, at line %d\n\nIn function:\n%s\n\nCondition:\n%s\n\nDescription:\n", file, line, func, code);

			strcat(messageboxBuffer, logfileBuffer);
			strcat(messageboxBuffer, "\n\nPress 'Abort' to kill application\nPress 'Retry' to break\nPress 'Ignore' to continue");

			int retVal = MessageBox(NULL, messageboxBuffer, "ViaEngine", MB_ABORTRETRYIGNORE | MB_ICONERROR);

			switch (retVal)
			{
			case IDABORT:
				{
					g_Kernel->Close();
					break;
				}

			case IDRETRY:
				{
					DebugBreak();
					break;
				}

			case IDIGNORE:
				{
					break;
				}
			}
		}
/*	}*/
}

/*--------------------------------------------------------------------------*/

void CLog::WriteLog(const char* file, int line, const char* func, int priority, const char* text)
{
	std::string f = ::GetFileFullname(file);

	time_t sectime;
	struct tm timeinfo;
	time(&sectime);
	localtime_s(&timeinfo, &sectime);

	char* letters = "IWEC";

	if (priority < 0)
	{
		priority = 0;
	}

	if (priority > 3)
	{
		priority = 3;
	}

	fprintf(m_log, "\"%02d:%02d:%02d$%c$%s$%s$%d$", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, letters[priority], func, f.c_str(), line);

	int i = 0;

	while (text[i] != '\0')
	{
		if (text[i] == '\n')
		{
			fprintf(m_log, "<br />");
		}
		else
		{
			fprintf(m_log, "%c", text[i]);
		}

		++i;
	}

	fprintf(m_log, "\",\n");
}

/*--------------------------------------------------------------------------*/
